<#assign classNameLower = ClassName ? uncap_first>
package ${pPackage}.service.impl;

import ${pPackage}.common.entity.PageResult;
import ${pPackage}.common.entity.ResultCode;
import ${pPackage}.common.exception.CommonException;

import ${pPackage}.common.utils.IdWorker;
import ${pPackage}.mapper.${ClassName}Mapper;
import ${pPackage}.pojo.${ClassName};
import ${pPackage}.service.I${ClassName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.util.List;

@Service
public class ${ClassName}Service implements I${ClassName}Service{

    @Autowired
    private ${ClassName}Mapper ${classNameLower}Mapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存
     */
    @Override
    public void add(${ClassName} ${classNameLower}) {
        String id = idWorker.nextId()+"";
        ${classNameLower}Mapper.insert(${classNameLower});
    }

    /**
     * 更新
     */
    @Override
    public void update(${ClassName} ${classNameLower})throws CommonException {
        ${ClassName} up${ClassName} = ${classNameLower}Mapper.selectByPrimaryKey(${classNameLower}.getId());
        if (up${ClassName} == null) {
        throw new CommonException(ResultCode.E_100104);
        }
        int i = ${classNameLower}Mapper.updateByPrimaryKeyAndVersion(${classNameLower});
        if (i == 0) {
            throw new CommonException(ResultCode.E_100116);
        }
    }

    /**
     * 删除
     */
    @Override
    public void deleteById(String id) throws CommonException {
        int i = ${classNameLower}Mapper.deleteByPrimaryKey(id);
        if (i == 0) {
            throw new CommonException(ResultCode.E_100104);
        }
    }

    /**
     * 根据id查询
     */
    @Override
    public ${ClassName} findById(String id) {
        return ${classNameLower}Mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult<${ClassName}> findPage(${ClassName} ${classNameLower}, Integer pageNum, Integer pageSize) throws CommonException {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(${ClassName}.class);
        Example.Criteria criteria = example.createCriteria();
        Class cls = ${classNameLower}.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
          Field f = fields[i];
          f.setAccessible(true);
          try {
            if (f.get(${classNameLower}) != null && f.get(${classNameLower}) != "") {
            criteria.andEqualTo(f.getName(), f.get(${classNameLower}));
            }
            } catch (Exception e) {
                throw new CommonException(ResultCode.FAIL);
            }
          }
          // 查询结果先按`id`字段排序，如果相同，则按`createTime`排序
          // example.setOrderByClause("`id` ASC,`createTime` ASC");
          List<${ClassName}> list = ${classNameLower}Mapper.selectByExample(example);

          PageInfo<${ClassName}> pageInfo = new PageInfo<>(list);

        return new PageResult<${ClassName}>(pageInfo.getList().size() == 0 ? 0 : pageInfo.getTotal(), pageInfo.getList());
      }
}
